﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcContrib.Pagination;

using ENail.Domain.Core;
using Equation.Enterprise.Framework.Domain.Model;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;
using ENail.Domain.Core.Contract.Services;


namespace ENail.Portal.Web.Models
{
    public class CustomerViewModel: 
        BaseViewModel<Int32>
    {
        [Required(ErrorMessage="Fist Name is empty")]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "Last Name is empty")]
        public string LastName { get; set; }
        public string Title { get; set; }
        public string BirthDay { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
    }

    public class CustomerListViewItem : BaseViewModel<Int32>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
        public string BirthDay { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public bool IsSelected { get; set; }
    }

    public class CustomerViewModelMapper: ViewModelMapper<CustomerViewModel, Customer>
    {
        public override void MapDomainToView(Customer domainModel, CustomerViewModel viewModel)
        {
 	        Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.Id = domainModel.Id;
            viewModel.FirstName = domainModel.FirstName;
            viewModel.LastName = domainModel.LastName;
            viewModel.Title = domainModel.Title;
            viewModel.State = domainModel.State;
            viewModel.BirthDay = domainModel.BirthDay;
            viewModel.Address = domainModel.Address;
            viewModel.City = domainModel.City;
            viewModel.State = domainModel.State;
            viewModel.Zip = domainModel.Zip;
            viewModel.Country  = domainModel.Country ;
            viewModel.Phone = domainModel.Phone;
            viewModel.Mobile = domainModel.Mobile;
            viewModel.Email = domainModel.Email;
        }

        public override void MapViewToDomain(CustomerViewModel viewModel, Customer domainModel)
        {
            Check.Require(viewModel != null);
            Check.Require(domainModel != null);

            domainModel.Id = viewModel.Id;
            domainModel.FirstName = viewModel.FirstName;
            domainModel.LastName = viewModel.LastName;
            domainModel.Title = viewModel.Title;
            domainModel.State = viewModel.State;
            domainModel.BirthDay = viewModel.BirthDay;
            domainModel.Address = viewModel.Address;
            domainModel.City = viewModel.City;
            domainModel.State = viewModel.State;
            domainModel.Zip = viewModel.Zip;
            domainModel.Country = viewModel.Country;
            domainModel.Phone = viewModel.Phone;
            domainModel.Mobile = viewModel.Mobile;
            domainModel.Email = viewModel.Email;
        }
    }

    public class CustomerListViewModelMapper : ListViewModelMapper<CustomerListViewItem, Customer>
    {
        public const int DEFAULT_PAGE = 1;
        public const int PAGE_SIZE = 3;

        public override void MapDomainToView(List<Customer> domainModel, GenericListViewModel<CustomerListViewItem> viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.List = (from e in domainModel
                              where
                                  e.IsActive &&
                                  (viewModel.Filter == null || viewModel.Filter.Trim() == "" ||
                                   e.FirstName.ToLower().Contains(viewModel.Filter.Trim().ToLower()) || e.LastName.ToLower().Contains(viewModel.Filter.Trim().ToLower()))
                              select new CustomerListViewItem
                                         {
                                             Id = e.Id,
                                             FirstName = e.FirstName,
                                             LastName = e.LastName,
                                             Title  = e.Title ,
                                             BirthDay = e.BirthDay,
                                             Address=e.Address,
                                             City=e.City,
                                             State=e.State,
                                             Zip=e.Zip,
                                             Country=e.Country,
                                             Phone = e.Phone,
                                             Mobile = e.Mobile,
                                             Email = e.Email,
                                    IsSelected = false
                              })
                              .OrderBy(x => x.FirstName)
                              .AsPagination(viewModel.Page ?? DEFAULT_PAGE, PAGE_SIZE);
        }

        public override void MapViewToDomain(GenericListViewModel<CustomerListViewItem> viewModel, List<Customer> domainModel)
        {
            throw new NotImplementedException();
        }
    }
}